package com.yogee.topseller.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yogee.topseller.R;
import com.yogee.topseller.activity.ProductDetailsActivity;
import com.yogee.topseller.activity.my.ButlerServiceActivity;
import com.yogee.topseller.bean.MySelfInfo;
import com.yogee.topseller.bean.SmallListBean;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @auathor ${XiaoJianghong}
 * @date 2017/7/24.
 */

public class ListRvAdapter extends RecyclerView.Adapter<ListRvAdapter.ItemHolder> {

    private Context context;
    private List<SmallListBean.ProductListBean> data;

    public ListRvAdapter(Context context) {
        this.context = context;
    }

    public void addData(List<SmallListBean.ProductListBean> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_is_list, parent, false);
        return new ItemHolder(view);
    }

    @Override
    public void onBindViewHolder(ItemHolder holder, int position) {
        final SmallListBean.ProductListBean bean = data.get(position);
        holder.itemListNameTv.setText(bean.getName());

        String labels = "";
        boolean isMj = false;

        holder.itemListDetailsTv.setText(bean.getProType());
        holder.itemListQtMoneyTv.setText(bean.getStartAmount());
        holder.itemListSyTv.setText(bean.getExpectReturn().substring(0,bean.getExpectReturn().length()-1));
        holder.itemListMonthTv.setText(bean.getInvestmentTerm());

        // 判断是否为会员
        if(MySelfInfo.getInstance().getIsPay().equals("0")){
            holder.itemListYongProTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
            holder.itemListYongProTv.setText(bean.getPercent() + "%");
            try{
                holder.itemListYongPb.setProgress(Integer.valueOf(bean.getPercent()));
            }catch (Exception e){
            }

        }else{
            holder.itemListYongProTv.setText("会员可见");
            holder.itemListYongProTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
            holder.itemListYongPb.setProgress(0);
        }
        holder.itemListYongBiTv.setText(bean.getCommission());


//        holder.itemListYongPb.setProgress(0);
        holder.itemListLl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                context.startActivity(new Intent(context, ProductDetailsActivity.class).putExtra("id", bean.getId()).putExtra("isFast", "1"));
            }
        });

        // 预热和图片
        if(bean.getImgstatus().equals("1")){
            holder.img_tu.setVisibility(View.VISIBLE);
        }else{
            holder.img_tu.setVisibility(View.GONE);
        }

        if(bean.getStatus().equals("预热中"))
            holder.img_yu.setVisibility(View.VISIBLE);
        else{
            holder.img_yu.setVisibility(View.GONE);
        }
        if(bean.getStatus().equals("售罄")){
            holder.img_sell_out.setVisibility(View.VISIBLE);
        }else
            holder.img_sell_out.setVisibility(View.GONE);
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    class ItemHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_list_name_tv)
        TextView itemListNameTv;
        @BindView(R.id.item_list_mj_tv)
        TextView itemListMjTv;
        @BindView(R.id.item_list_details_tv)
        TextView itemListDetailsTv;
        @BindView(R.id.item_list_qt_money_tv)
        TextView itemListQtMoneyTv;
        @BindView(R.id.item_list_sy_tv)
        TextView itemListSyTv;
        @BindView(R.id.item_list_month_tv)
        TextView itemListMonthTv;
        @BindView(R.id.item_list_yong_bi_tv)
        TextView itemListYongBiTv;
        @BindView(R.id.item_list_yong_pro_tv)
        TextView itemListYongProTv;
        @BindView(R.id.item_list_yong_pb)
        ProgressBar itemListYongPb;
        @BindView(R.id.item_list_ll)
        LinearLayout itemListLl;
        @BindView(R.id.img_tu)
        ImageView img_tu;
        @BindView(R.id.img_yu)
        ImageView img_yu;
        @BindView(R.id.img_sell_out)
        ImageView img_sell_out;

        public ItemHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
